package two.chapter_1.c1_2;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdRandom;

public class Point2D {
    private double x, y;

    public Point2D(double x, double y) {
        if (Double.isInfinite(x) || Double.isInfinite(y)) {
            throw new IllegalArgumentException("Coordinates must be finite");
        }
        if (Double.isNaN(x) || Double.isNaN(y)) {
            throw new IllegalArgumentException("Coordinates cannot be NaN");
        }
        if (x == 0.0) this.x = 0.0;  // convert -0.0 to +0.0
        else this.x = x;

        if (y == 0.0) this.y = 0.0;  // convert -0.0 to +0.0
        else this.y = y;

    }

    public double distanceTo(Point2D that) {
        double x2 = (that.x - x) * (that.x - x);
        double y2 = (that.y - y) * (that.y - y);
        return Math.sqrt(x2 + y2);
    }

    public void draw() {
        StdDraw.point(x, y);
    }

    public double x() {
        return x;
    }

    public double y() {
        return y;
    }
}

